package com.spark.incoicings.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spark.incoicings.service.*;
import com.spark.incoicings.vo.OrderDetailVo;
import com.spark.incoicings.vo.PurchaseDetailVo;
import com.spark.invoicings.entity.*;
import com.spark.invoicings.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * 应收/应付表
 *
 */
@RestController
@RequestMapping("/SalesReceipt")
public class ReceivablePayableDetailController {
    @Resource
    private ReceivablePayableDetailService rps;
    @Resource
    private DictDataService ds;
    @Resource
    private ProcuredSupplierService pss;
    @Resource
    private SaleCustomerService sc;
    @Resource
    private ProductService ps;
    @Resource
    private OutboundDetailService outds;
    @Resource
    private OutboundOrderService oos;
    @Resource
    private InboundDetailService ids;
    @Resource
    private SalesOrderService so;
    @Resource
    private OrderDetailService ods;
    @Resource
    private OrderService os;
    @Resource
    private PurchaseDetailService pds;
    @Resource
    private WarehouseReceiptService wrs;

    /**
     * 应收应付条件分页查询
     * */
    @GetMapping("/select")
    public Result select(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "6") Integer pageSize,
            @RequestParam(value = "businessTypeId", required = false) Integer businessTypeId) {

        PageHelper.startPage(pageNum, pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("businessTypeId", businessTypeId);
        PageHelper.orderBy("receivable_payable_detail_id desc");
        List<ReceivablePayableDetail> list = rps.select(map);

        for (ReceivablePayableDetail r : list) {
            if (r.getCustomerId() != null) {
                r.setSaleCustomer(sc.getById(r.getCustomerId()));
            }
            if (r.getSupplierId() != null) {
                r.setProcuredSupplier(pss.getById(r.getSupplierId()));
            }
            if (r.getProductId() != null) {
                r.setProduct(ps.getById(r.getProductId()));
            }
            if (r.getOutboundDetailId() != null) {
                r.setOutboundDetail(outds.getById(r.getOutboundDetailId()));
                OutboundDetail outboundDetail = r.getOutboundDetail();
                if (outboundDetail != null) {
                    if (outboundDetail.getOutboundOrderId() != null) {
                        outboundDetail.setOutboundOrder(oos.getById(outboundDetail.getOutboundOrderId()));
                    }
                    if (outboundDetail.getProductId() != null) {
                        outboundDetail.setProduct(ps.getById(outboundDetail.getProductId()));
                    }
                }
            }
            if (r.getInboundDetailId() != null) {
                r.setInboundDetail(ids.getById(r.getInboundDetailId()));
                InboundDetail inboundDetail = r.getInboundDetail();
                if (inboundDetail != null) {
                    if (inboundDetail.getInboundOrderId() != null) {
                        inboundDetail.setWarehouseReceipt(wrs.getById(inboundDetail.getInboundOrderId()));
                    }
                    if (inboundDetail.getProductId() != null) {
                        inboundDetail.setProduct(ps.getById(inboundDetail.getProductId()));
                    }
                }
            }
            if (r.getOrderDetailId() != null) {
                r.setOrderDetail(ods.getById(r.getOrderDetailId()));
            }
            if (r.getSalesOrderId() != null) {
                r.setSalesOrder(so.getById(r.getSalesOrderId()));
            }
            if (r.getPurchaseDetailId() != null) {
                r.setPurchaseDetail(pds.getById(r.getPurchaseDetailId()));
            }
            if (r.getPurchaseOrderId() != null) {
                r.setOrder(os.getById(r.getPurchaseOrderId()));
            }
            if (r.getSettlementTypeId() != null) {
                r.setDictData(Arrays.asList(ds.getById(r.getSettlementTypeId()), ds.getById(r.getBusinessTypeId())));
            }
        }

        PageInfo<ReceivablePayableDetail> page = new PageInfo<>(list);
        return Result.success(200, "success", page);
    }


    /**
     * 查询商品
     * */
    @GetMapping("/selectProduct")
    public Result selectProduct(){
        List<Product> list = ps.list();
        return  Result.success(200,"success",list);
    }

    /**
     * 根据应收/应付明细id查询对应数据
     * */
    @GetMapping("/selectRId")
    public Result selectRId(Integer receivablePayableDetailId){
        ReceivablePayableDetail r = rps.getById(receivablePayableDetailId);
        if (r.getCustomerId() != null) {
            r.setSaleCustomer(sc.getById(r.getCustomerId()));
        }
        if (r.getSupplierId() != null) {
            r.setProcuredSupplier(pss.getById(r.getSupplierId()));
        }
        if (r.getProductId() != null) {
            r.setProduct(ps.getById(r.getProductId()));
        }
        if (r.getOutboundDetailId() != null) {
            r.setOutboundDetail(outds.getById(r.getOutboundDetailId()));
            OutboundDetail outboundDetail = r.getOutboundDetail();
            if (outboundDetail != null) {
                if (outboundDetail.getOutboundOrderId() != null) {
                    outboundDetail.setOutboundOrder(oos.getById(outboundDetail.getOutboundOrderId()));
                }
                if (outboundDetail.getProductId() != null) {
                    outboundDetail.setProduct(ps.getById(outboundDetail.getProductId()));
                }
            }
        }
        if (r.getInboundDetailId() != null) {
            r.setInboundDetail(ids.getById(r.getInboundDetailId()));
            InboundDetail inboundDetail = r.getInboundDetail();
            if (inboundDetail != null) {
                if (inboundDetail.getInboundOrderId() != null) {
                    inboundDetail.setWarehouseReceipt(wrs.getById(inboundDetail.getInboundOrderId()));
                }
                if (inboundDetail.getProductId() != null) {
                    inboundDetail.setProduct(ps.getById(inboundDetail.getProductId()));
                }
            }
        }
        if (r.getOrderDetailId() != null) {
            r.setOrderDetail(ods.getById(r.getOrderDetailId()));
        }
        if (r.getSalesOrderId() != null) {
            r.setSalesOrder(so.getById(r.getSalesOrderId()));
        }
        if (r.getPurchaseDetailId() != null) {
            r.setPurchaseDetail(pds.getById(r.getPurchaseDetailId()));
        }
        if (r.getPurchaseOrderId() != null) {
            r.setOrder(os.getById(r.getPurchaseOrderId()));
        }
        if (r.getSettlementTypeId() != null) {
            r.setDictData(Arrays.asList(ds.getById(r.getSettlementTypeId()), ds.getById(r.getBusinessTypeId())));
        }
        return Result.success(200,"success",r);
    }

    /**
     * 前端需要的数据
     * */
    @GetMapping("/salesOrdersCId")
    public Result salesOrdersCId(Integer customerId){
        List<SalesOrder> salesOrders = so.selectId(customerId);
        for (SalesOrder salesOrder : salesOrders) {
            salesOrder.setOrderDetail(ods.selectODetail(salesOrder.getOrderId()));

            for (OrderDetail s:salesOrder.getOrderDetail()){
                s.setOutboundDetails(outds.selectOutbound(s.getOrderDetailId()));
            }
        }
        return Result.success(200, "success", salesOrders);
    }

    @GetMapping("/ordersSId")
    public Result ordersSId(Integer supplierId){
        List<Order> orders = os.selectOrderId(supplierId);
        for (Order o:orders){
            o.setPurchaseDetails(pds.selectOrderDetail(o.getPurchaseOrderId()));
            for (PurchaseDetail p:o.getPurchaseDetails()){
                p.setInboundDetails(ids.selectInbound(p.getPurchaseDetailId()));
            }
        }
        return Result.success(200, "success", orders);
    }

    /**
     * 添加应收/应付
     * */
    @PostMapping("/saveReceivablePayable")
    public Result saveReceivablePayable(@RequestBody ReceivablePayableDetail r){
        if(r.getOrderDetailId()!=null && r.getOrderDetailId() > 0){
            OrderDetail o = ods.getById(r.getOrderDetailId());
            r.setQuantity(o.getQuantityOrdered());//数量
            r.setUnitPrice(o.getDiscountedPrice());//单价
            r.setReceivableAmount(o.getSubtotalAmount());//应收金额
            r.setPendingCollectionTotalAmount(o.getPendingPaymentAmount());//待收金额
            r.setReceivedTotalAmount(o.getSubtotalAmount()-o.getPendingPaymentAmount());//已收总额
            r.setGeneratedStatement(0);
        }
        if(r.getPurchaseDetailId()!=null && r.getPurchaseDetailId()>0){
            PurchaseDetail p = pds.getById(r.getPurchaseDetailId());
            r.setQuantity(p.getPurchaseQuantity());//数量
            r.setUnitPrice(p.getPurchasePrice());//单价
            r.setPayableTotalAmount(p.getPayableAmount());//应付总额
            r.setPendingPaymentTotalAmount(p.getPendingPaymentAmount());//待付金额
            r.setPaidTotalAmount(p.getPayableAmount()-p.getPendingPaymentAmount());//已付金额
            r.setGeneratedStatement(0);
        }

        rps.save(r);
        return Result.success();
    }

    /**
     * 修改应收/应付
     * */
    @PostMapping("/updateReceivablePayable")
    public Result updateReceivablePayable(@RequestBody ReceivablePayableDetail receivablePayableDetail){
        rps.updateById(receivablePayableDetail);
        return Result.success();
    }

    /**
     * 根据客户id查询应收应付单
     * */
    @GetMapping("/selectSaleCustomerId")
    public Result selectSaleCustomerId(Integer customerId){
        List<ReceivablePayableDetail> list = rps.selectId(customerId);
        for (ReceivablePayableDetail r:list){
            r.setProduct(ps.getById(r.getProductId()));
            r.setDictData(Arrays.asList(ds.getById(r.getSettlementTypeId()), ds.getById(r.getBusinessTypeId())));
        }
        return Result.success(200,"success",list);
    }

    /**
     * 根据供应商id查询应收应付单
     * */
    @GetMapping("/selectSupplierId")
    public Result selectSupplierId(Integer selectSupplierId){
        List<ReceivablePayableDetail> list = rps.selectPayment(selectSupplierId);
        for (ReceivablePayableDetail r:list){
            r.setProduct(ps.getById(r.getProductId()));
            r.setDictData(Arrays.asList(ds.getById(r.getSettlementTypeId()), ds.getById(r.getBusinessTypeId())));
        }
        return Result.success(200,"success",list);
    }

    /**
     * 销售需求分析
     * */
    @GetMapping("/selectOrderDetailVo")
    public Result selectOrderDetailVo(){
        List<Object> list=new ArrayList<>();
        List<Integer> productIds = ods.selectProductIds();
        for (Integer p:productIds){
            List<OrderDetailVo> os = ods.selectOrderDetailVo(p);
            list.add(os);
        }
        return Result.success(200,"success",list);
    }

    /**
     * 采购在途情况
     * */
    @GetMapping("/selectPurchaseDetailVo")
    public Result selectPurchaseDetailVo(){
        List<Object> list=new ArrayList<>();
        List<Integer> productIds = pds.selectProductIds();
        for (Integer p:productIds){
            List<PurchaseDetailVo> po = pds.selectPurchaseDetailVo(p);
            list.add(po);
        }
        return Result.success(200,"success",list);
    }
}
